// 店铺销量排行榜
<template>
    <div class="gradient-content store full-width full-height">
        <div :id="idName"></div>
    </div>
</template>

<script>
import { Chart } from '@antv/g2'
export default {
    props: {
        idName: {
            type: String,
            default: 'storeSalesRanking',
        },
        chartData: {
            type: Array,
            default: () => [],
        },
    },
    watch: {
        chartData(val) {
            this.chart.changeData(val)
        },
    },
    data() {
        return {
            chart: null,
            height: 0,
        }
    },
    mounted() {
        this.$nextTick(() => {
            const boxContElement = document.querySelector(`.gradient-content.store`)
            this.height = boxContElement.clientHeight
            this.render()
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.destroy()
    },
    methods: {
        render() {
            // Step 1: 创建 Chart 对象
            this.chart = new Chart({
                container: this.idName,
                height: this.height,
                autoFit: true,
                padding: [15, 70, 15, 120],
            })
            // Step 2: 载入数据源
            this.chart.data(this.chartData)
            this.chart.axis('shop_name', {
                title: null,
                tickLine: null,
                line: null,
            })
            this.chart.axis('total_price', {
                label: null,
                title: {
                    offset: 30,
                    style: {
                        fontSize: 12,
                        fontWeight: 300,
                    },
                },
            })
            // Step 3：创建图形语法，绘制条形图，由 total_price 和 shop_name 两个属性决定图形位置，shop_name 映射至 x 轴，total_price 映射至 y 轴
            this.chart.legend(false)
            this.chart.coordinate().transpose()
            
            this.chart
                .interval()
                .position('shop_name*total_price')
                .size(26)
                .label('total_price', {
                    style: {
                        fill: '#fff',
                    },
                    offset: 10,
                })
            // .color('shop_name')
            this.chart.interaction('element-active')

            // Step 4: 渲染图表
            this.chart.render()
        },
    },
}
</script>
